# -*- coding: utf-8 -*-

'''
/**
 * @file        dpt_coder.py
 * @brief       XXXX
 * @note        XXXX
 * @author      靳普诏(puzhao.jin@hopechart.com)
 * @date        2024/05/18
 * @version     1.0

 * @par         修改日志
 * <table>
 * <tr><th>Date         <th>Version     <th>Author      <th> Description
 * <tr><td>2024/05/18   <td>1.0         <td>靳普诏       <td> 创建初始版本
 * @copyright   xxxx
 */
'''

import sys
import os
import argparse

from datetime import datetime
# 获取当前日期
CURR_DATE_STRING = datetime.now().strftime("%Y-%m-%d")
# 获取当前时间
CURR_TIME_STRING = datetime.now().strftime("%H:%M:%S")
## 获取当前脚本所在的目录
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
# 输出目录路径
# print("SCRIPT_DIR: "+ SCRIPT_DIR)



import struct



class TMem:
    def __init__(self, buffer:bytes):
        self.buffer = buffer
    def __len__(self):
        return len(self.buffer)
    def __getitem__(self, index):
        return self.buffer[index]
    def __setitem__(self, index, value):
        self.buffer[index] = value
    def __str__(self):
        return str(self.buffer)
    @property
    def ptr(self):
        return self.buffer
    @property
    def size(self):
        return len(self.buffer)


class TDptEncoder:
    def __init__(self):
        self._buffer = bytearray()
        pass

    @property
    def buffer(self):
        return self._buffer
    
    def WritePosition(self):
        return len(self._buffer)
    
    def WriteUInt8(self, value, is_big_endian=True):
        # 打包一个无符号8位整数
        value &= 0xFF
        if is_big_endian:
            self._buffer.extend(struct.pack('>B', value))
        else:
            self._buffer.extend(struct.pack('<B', value))
    
    def WriteInt8(self, value, is_big_endian=True):
        value &= 0xFF
        # 打包一个有符号8位整数
        if is_big_endian:
            self._buffer.extend(struct.pack('>b', value))
        else:
            self._buffer.extend(struct.pack('<b', value))
    
    def WriteUInt16(self, value, is_big_endian=True):
        value &= 0xFFFF
        # 打包一个无符号16位整数
        if is_big_endian:
            self._buffer.extend(struct.pack('>H', value))
        else:
            self._buffer.extend(struct.pack('<H', value))
    
    def WriteInt16(self, value, is_big_endian=True):
        value &= 0xFFFF
        # 打包一个有符号16位整数
        if is_big_endian:
            self._buffer.extend(struct.pack('>h', value))
        else:
            self._buffer.extend(struct.pack('<h', value))
    
    def WriteUInt32(self, value, is_big_endian=True):
        value &= 0xFFFFFFFF
        # 打包一个无符号32位整数
        if is_big_endian:
            self._buffer.extend(struct.pack('>I', value))
        else:
            self._buffer.extend(struct.pack('<I', value))
    
    def WriteInt32(self, value, is_big_endian=True):
        value &= 0xFFFFFFFF
        # 打包一个有符号32位整数
        if is_big_endian:
            self._buffer.extend(struct.pack('>i', value))
        else:
            self._buffer.extend(struct.pack('<i', value))
    
    def WriteUInt64(self, value, is_big_endian=True):
        value &= 0xFFFFFFFFFFFFFFFF
        # 打包一个无符号64位整数
        if is_big_endian:
            self._buffer.extend(struct.pack('>Q', value))
        else:
            self._buffer.extend(struct.pack('<Q', value))
    
    def WriteInt64(self, value, is_big_endian=True):
        value &= 0xFFFFFFFFFFFFFFFF
        # 打包一个有符号64位整数
        if is_big_endian:   
            self._buffer.extend(struct.pack('>q', value))
        else:
            self._buffer.extend(struct.pack('<q', value))
    
    def WriteMem(self, value:bytes):
        # 打包一个TMem对象
        self._buffer.extend(value)


class TDptDecoder:
    def __init__(self, buffer:bytes):
        self._buffer = buffer
        self._pos = 0
        pass
    
    @property
    def buffer(self):
        return self._buffer[self._pos : ]
    
    def ReadPosition(self):
        return self._pos
    
    def ReadUInt8(self, is_big_endian=True):
        # 读取一个无符号8位整数
        if is_big_endian:
            value = struct.unpack('>B', self._buffer[self._pos:self._pos+1])[0]
        else:
            value = struct.unpack('<B', self._buffer[self._pos:self._pos+1])[0]
        self._pos += 1
        return value
    
    def ReadInt8(self, is_big_endian=True):
        # 读取一个有符号8位整数
        if is_big_endian:
            value = struct.unpack('>b', self._buffer[self._pos:self._pos+1])[0]
        else:
            value = struct.unpack('<b', self._buffer[self._pos:self._pos+1])[0]
        self._pos += 1
        return value
    
    def ReadUInt16(self, is_big_endian=True):
        # 读取一个无符号16位整数
        if is_big_endian:
            value = struct.unpack('>H', self._buffer[self._pos:self._pos+2])[0]
        else:
            value = struct.unpack('<H', self._buffer[self._pos:self._pos+2])[0]
        self._pos += 2
        return value
    
    def ReadInt16(self, is_big_endian=True):
        # 读取一个有符号16位整数
        value = struct.unpack('>h', self._buffer[self._pos:self._pos+2])[0]  
        self._pos += 2
        return value
    
    def ReadUInt32(self, is_big_endian=True):
        # 读取一个无符号32位整数
        value = struct.unpack('>I', self._buffer[self._pos:self._pos+4])[0]
        self._pos += 4
        return value
    
    def ReadInt32(self, is_big_endian=True):
        # 读取一个有符号32位整数
        value = struct.unpack('>i', self._buffer[self._pos:self._pos+4])[0]
        self._pos += 4
        return value
    
    def ReadUInt64(self, is_big_endian=True):
        # 读取一个无符号64位整数
        value = struct.unpack('>Q', self._buffer[self._pos:self._pos+8])[0]
        self._pos += 8
        return value
    
    def ReadInt64(self, is_big_endian=True):
        # 读取一个有符号64位整数
        value = struct.unpack('>q', self._buffer[self._pos:self._pos+8])[0]
        self._pos += 8
        return value
    
    def ReadMem(self, size):
        # 读取一个TMem对象
        value = bytes(self._buffer[self._pos:self._pos+size])
        self._pos += size
        return value



if __name__ == "__main__":


    ## 获取脚本传参
    argv = sys.argv
    argc = len(argv)
    # 打印命令行参数
    #for i in range(1, argc):
        #print("参数", i, ":", argv[i])


    # 设置命令行参数解析器
    # parser = argparse.ArgumentParser(description='Filter log file based on keywords.')
    # parser.add_argument('input_file', type=str, help='Input log file path')
    # parser.add_argument('output_file', type=str, help='Output filtered log file path')
    # parser.add_argument('filter_list', nargs='+', type=str, help='List of keywords to filter')

    # 解析命令行参数
    # args = parser.parse_args()

        
    print("============= BIGEN: dpt_coder.py =============")



    print("============= END: dpt_coder.py =============")



## Generated on "2024-05-18 19:46:28" by the tool "gen_hq_file.py >> V20231119" 

